﻿namespace NTLib.Pipe.Windows.Descriptors
{
    using Core.Descriptors;
    using Streaming.Descriptors;
    using System;
    using Pipe.Descriptors;

    /// <summary>
    /// Describe the needed information for create a NamedPipeSource
    /// </summary>
    /// <seealso cref="NTLib.Core.Descriptors.Descriptor" />
    /// <seealso cref="NTLib.Pipe.Descriptors.ITargetDescriptor" />
    public class NamedPipeTargetDescriptor : StreamingTargetDescriptor
    {
        #region Ctor

        /// <summary>
        /// Initializes a new instance of the <see cref="NamedPipeTargetDescriptor"/> class.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <param name="formater"></param>
        /// <param name="name">The name.</param>
        public NamedPipeTargetDescriptor(Guid id, 
                                         IDescriptor formater,
                                         bool isServerHost,
                                         string pipeName,
                                         TimeSpan? connectionTimeOut = null,
                                         string name = null) 
            : base(id, formater, name)
        {
            this.IsServerHost = isServerHost;
            this.PipeName = pipeName;
            this.ConnectionTimeOut = connectionTimeOut;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets a value indicating whether this instance is server host.
        /// </summary>
        /// <value>
        /// <c>true</c> if this instance is server host; otherwise, <c>false</c>.
        /// </value>
        public bool IsServerHost { get; }

        /// <summary>
        /// Gets the name of the pipe.
        /// </summary>
        /// <value>
        /// The name of the pipe.
        /// </value>
        public string PipeName { get; }

        /// <summary>
        /// Gets the connection time out.
        /// </summary>
        /// <value>
        /// The connection time out.
        /// </value>
        public TimeSpan? ConnectionTimeOut { get; }

        #endregion

        #region Methods

        /// <summary>
        /// Creates the new clone instance.
        /// </summary>
        protected override IDescriptor CreateCloneNewInstance()
        {
            return new NamedPipeTargetDescriptor(this.Id,
                                                 this.PackageFormater,
                                                 this.IsServerHost,
                                                 this.PipeName,
                                                 this.ConnectionTimeOut,
                                                 this.Name);
        }

        /// <summary>
        /// Called when the <see cref="BaseTargetDescription.Equals(ITargetDescriptor)" /> method is called.
        /// All the basic check have already been check
        /// when the <see cref="BaseTargetDescription.OnEquals(ITargetDescriptor)" />
        /// is called.
        /// </summary>
        /// <param name="other"></param>
        protected override bool OnEquals(ITargetDescriptor other)
        {
            NamedPipeTargetDescriptor info = other as NamedPipeTargetDescriptor;
            return base.OnEquals(other) &&
                   this.IsServerHost == info.IsServerHost &&
                   string.Equals(this.PipeName, info.PipeName) &&
                   this.ConnectionTimeOut == info.ConnectionTimeOut;
        }

        #endregion
    }
}
